package com.funsince.amiable.dao;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.funsince.amiable.entity.BaseBean;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class BaseDao {
	protected DBManager dbHelper;
	SQLiteDatabase dbHelper1;
	public BaseDao(Context context){
		this.dbHelper=new DBManager(context);
		dbHelper.openDatabase();
		dbHelper.closeDatabase();
		dbHelper1 = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
	}
	
	/**
	 * 查询 1
	 */
	public HashMap<String,String> cursorToBean1(Cursor cursor) {
		HashMap<String,String> maps = new HashMap<String,String>();
		String[] columnName = cursor.getColumnNames();
		for (int k = 0; k < columnName.length; k++) {
			maps.put(columnName[k], cursor.getString(cursor.getColumnIndex(columnName[k])));
		}
		return maps;
	}
	/**
	 * 修改
	 */
	public boolean updateBybean(BaseBean bean,String tableName,String id) {
		List<String> l1 = new ArrayList<String>();
		Object[] os = null;
		Object[] oss = null;
		Object[] ls = new Object[1];
		String whereClause = "";
		String up_id = null;
		Field[] fields = null;
		int c = 1;
		int n = 0;
		try {
			 fields = bean.getClass().getDeclaredFields();
			os = new Object[fields.length];
			for (int j = 0; j < fields.length; j++) {
				Field field = fields[j];
				String fieldName = field.getName();
				String fieldNameD = fieldName.substring(0, 1).toUpperCase()
						+ fieldName.substring(1);
				String getMethodName = "get" + fieldNameD;
				Method getMethod = bean.getClass().getMethod(getMethodName);

				if (fieldName.equals(id)) {
					whereClause = fieldName +" = ?";
					ls[0] = getMethod.invoke(bean);
				} else {
					if(getMethod.invoke(bean) != null){
						l1.add(fieldName+" = ?");
						os[j] = getMethod.invoke(bean);
						++c;
					}
				}
			}
		} catch (Exception e) {
			
		}
		oss = new Object[c];
		for(int k=0;k<fields.length;k++){
			if(os[k] != null){
				oss[n]=os[k];
				++n;
			}
		}
		String fs;
		fs = l1.toString().substring(1, l1.toString().length()-1);
		oss[c-1] = ls[0];
		dbHelper1.execSQL("update "+tableName+" set "+fs+" where "+whereClause, oss);
		Log.e("aaaaaaaaaaaa", "update "+tableName+" set "+fs+" where "+whereClause+ oss+"");
		return true;
	}
	/**
	 * 通过id删除 
	 */
	public Boolean deleteById(String tableName,String str,String id){
		int num = dbHelper1.delete(tableName, str, new String[]{id});
		if(num > 0){
			return true;
		}
		return false;
	}
	/**
	 * 添加 
	 */
	public boolean insertBybean(BaseBean bean,String tableName) {
		List<String> l1 = new ArrayList<String>();
		List<String> l2 = new ArrayList<String>();
		Object[] os = null;
		Field[] fields = null;
		try {
			fields = bean.getClass().getDeclaredFields();
			Log.e("-0------------------",fields[1]+""+fields[2]+"");
			os = new Object[fields.length];
			for (int j = 0; j < fields.length; j++) {
				Field field = fields[j];
				String fieldName = field.getName();
				String fieldNameD = fieldName.substring(0, 1).toUpperCase()
						+ fieldName.substring(1);
				String getMethodName = "get" + fieldNameD;
				Method getMethod = bean.getClass().getMethod(getMethodName);
				l1.add(fieldName);
				l2.add("?");
				os[j] = getMethod.invoke(bean);
			}
		} catch (Exception e) {
			
		}
		String fs;
		fs = l1.toString().substring(1, l1.toString().length()-1);
		String vs;
		vs = l2.toString().substring(1, l2.toString().length()-1);
		dbHelper1.execSQL("insert into "+tableName+"("+fs+") values ("+vs+")", os);
		Log.e("aaaaaa","insert into "+tableName+"("+fs+") values ("+vs+")"+os);
		return true;
	}

}
